//
//  BaseWebViewController.m
//  WuCha
//
//  Created by michael shen on 16/3/6.
//  Copyright © 2016年 youzi. All rights reserved.
//

#import "BaseWebViewController.h"
#import "NJKWebViewProgressView.h"
//#import "FoodDetailViewController.h"
//#import "UITableViewTestViewController.h"

@interface BaseWebViewController()
@property (nonatomic,strong) UIWebView *webView;
@property (nonatomic,strong) NJKWebViewProgressView *progressView;
@property (nonatomic,strong) NJKWebViewProgress *progressProxy;
@end

@implementation BaseWebViewController
-(void)viewDidLoad{
    [super viewDidLoad];
//    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44+StatusBarHeight,SCREEN_WIDTH, SCREEN_HEIGHT-44-StatusBarHeight)];
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];
//    _webView.scalesPageToFit = YES;
    _progressProxy = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _progressProxy;
    _progressProxy.webViewProxyDelegate = self;
    _progressProxy.progressDelegate = self;
    
    CGFloat progressBarHeight = 4.f;
//    CGRect navigationBarBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0, 0, SCREEN_WIDTH, progressBarHeight);
    _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
//    _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_progressView setProgressBarColor:DefaultMigrantBlue];
    [_progressView setProgress:0 animated:YES];

//    [self loadGoogle];
}

-(void)loadLocalHTML:(NSString*)htmlString baseUrl:(NSURL*)baseUrl{
    [self.webView loadHTMLString:htmlString baseURL:baseUrl];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.webView addSubview:_progressView];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    // Remove progress view
    // because UINavigationBar is shared with other ViewControllers
    [_progressView removeFromSuperview];
}

-(void)loadHtml
{
    NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.urlString]];
    [_webView loadRequest:req];
}

#pragma mark - NJKWebViewProgressDelegate
-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    NSLog(@"---%f",progress);
    [_progressView setProgress:progress animated:YES];
//    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSString *requestString = [[[request URL]  absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
    NSLog(@"----------%@",requestString);
    if ([requestString hasPrefix:@"myapp:"]) {
        NSLog(@"requestString:%@",requestString);
        NSArray *params = [requestString componentsSeparatedByString:@":"];
        NSString *paramString = params[1];
        if ([paramString isEqualToString:@"userReg"]) {
            //实名认证成功
            [self showMessage:@"实名认证成功" autoDissmissAfterDelay:0];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadUserInfo" object:@""];
            [self performBlock:^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            } afterDelay:0.8];
        }else if ([paramString isEqualToString:@"netSave"]){
            //充值成功
            [self showMessage:@"充值成功" autoDissmissAfterDelay:0];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadUserInfo" object:@""];
            [self performBlock:^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            } afterDelay:0.8];
        }else if ([paramString isEqualToString:@"cash"]){
            //充值成功
            [self showMessage:@"取现成功" autoDissmissAfterDelay:0];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadUserInfo" object:@""];
            [self performBlock:^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            } afterDelay:0.8];
        }else if ([paramString isEqualToString:@"initiativeTender"]){
            [self showMessage:@"购买成功" autoDissmissAfterDelay:0];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"ReloadHomeUserInfo" object:@""];

            [self performBlock:^{
                [self.navigationController popToRootViewControllerAnimated:YES];
            } afterDelay:0.8];
        }
//        //如果是自己定义的协议, 再截取协议中的方法和参数, 判断无误后在这里手动调用oc方法
//        NSRange range1 = [requestString rangeOfString:@"&"];
//        requestString = [requestString substringFromIndex:range1.location+1];
//        NSMutableDictionary *param = [self queryStringToDictionary:requestString];
//        NSLog(@"get param:%@",[param description]);
//        
//        NSString *func = [param objectForKey:@"func"];
//        
//        if([func isEqualToString:@"orderFood"])//订餐
//        {
//            FoodDetailViewController *foodDetailController = [[FoodDetailViewController alloc] init];
//            foodDetailController.foodId = [NSString stringWithFormat:@"%@",param[@"skuId"]];
//            
//            [self.navigationController pushViewController:foodDetailController animated:YES];
//        }
        
        return NO;
    }
    return YES;
}
//
//- (NSMutableDictionary*)queryStringToDictionary:(NSString*)string {
//    NSMutableArray *elements = (NSMutableArray*)[string componentsSeparatedByString:@"&"];
//    NSMutableDictionary *retval = [NSMutableDictionary dictionaryWithCapacity:[elements count]];
//    for(NSString *e in elements) {
//        NSArray *pair = [e componentsSeparatedByString:@"="];
//        [retval setObject:[pair objectAtIndex:1]?:@"" forKey:[pair objectAtIndex:0]?[pair objectAtIndex:0]:@"nokey"];
//    }
//    return retval;
//}

@end
